package com.platform.usercenter.service.impl;

import com.platform.common.core.domain.entity.SysDept;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.usercenter.domain.SysUserAirport;
import com.platform.usercenter.domain.SysUserDept;
import com.platform.usercenter.domain.SysUserPost;
import com.platform.usercenter.domain.SysUserRole;
import com.platform.usercenter.domain.query.SysUserDeptQuery;
import com.platform.usercenter.domain.vo.UserDeptVo;
import com.platform.usercenter.mapper.*;
import com.platform.usercenter.service.ISysUserDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户与部门关联Service业务层处理
 *
 * @author platform
 * @date 2023-03-30
 */
@Service
public class SysUserDeptServiceImpl implements ISysUserDeptService {
    @Resource
    private SysUserDeptMapper sysUserDeptMapper;
    @Resource
    private SysUserAirportMapper userAirportMapper;
    @Resource
    private SysUserPostMapper userPostMapper;
    @Resource
    private SysUserRoleMapper userRoleMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询用户与部门关联
     *
     * @param userId 用户与部门关联主键
     * @return 用户与部门关联
     */
    @Override
    public List<SysUserDept> selectSysUserDeptByUserId(Long userId) {
        return sysUserDeptMapper.selectSysUserDeptByUserId(userId);
    }

    /**
     * 查询用户与部门关联列表
     *
     * @param query 用户与部门关联
     * @return 用户与部门关联
     */
    @Override
    public List<SysUserDept> selectSysUserDeptList(SysUserDeptQuery query) {
        return sysUserDeptMapper.selectSysUserDeptList(query);
    }

    /**
     * 新增用户与部门关联
     *
     * @param sysUserDept 用户与部门关联
     * @return 结果
     */
    @Override
    public int insertSysUserDept(SysUserDept sysUserDept) {
        return sysUserDeptMapper.insertSysUserDept(sysUserDept);
    }

    /**
     * 修改用户与部门关联
     *
     * @param sysUserDept 用户与部门关联
     * @return 结果
     */
    @Override
    public int updateSysUserDept(SysUserDept sysUserDept) {
        return sysUserDeptMapper.updateSysUserDept(sysUserDept);
    }

    /**
     * 批量删除用户与部门关联
     *
     * @param userIds 需要删除的用户与部门关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserDeptByUserIds(Long[] userIds) {
        return sysUserDeptMapper.deleteSysUserDeptByUserIds(userIds);
    }

    /**
     * 删除用户与部门关联信息
     *
     * @param userId 用户与部门关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserDeptByUserId(Long userId) {
        return sysUserDeptMapper.deleteSysUserDeptByUserId(userId);
    }

    @Override
    public List<UserDeptVo> selectUserDeptVoList(Long userId, Long optUserId, Long loginDeptId) {
        List<UserDeptVo> userDeptVoList = new ArrayList<>();
        List<SysUserDept> sysUserDepts = sysUserDeptMapper.selectSysUserDeptByUserId(userId);
        List<SysUserAirport> sysUserAirports = userAirportMapper.selectSysUserAirportByUserId(userId);
        List<SysUserPost> sysUserPosts = userPostMapper.selectSysUserPostByUserId(userId);
        List<SysUserRole> sysUserRoles = userRoleMapper.selectSysUserRoleByUserId(userId);
        List<SysDept> deptList = sysDeptMapper.selectNormalChildrenDeptListById(loginDeptId);
        if (sysUserDepts != null) {
            for (SysUserDept sysUserDept : sysUserDepts) {
                UserDeptVo userDeptVo = new UserDeptVo();
                userDeptVo.setDeptId(sysUserDept.getDeptId());
                userDeptVo.setIsDefault(sysUserDept.getIsDefault());
                userDeptVo.setUserDeptType(sysUserDept.getUserDeptType());
                String[] airportCodes = (String[]) sysUserAirports.stream().filter(r -> r.getDeptId().equals(sysUserDept.getDeptId()))
                        .map(SysUserAirport::getAirportCode).collect(Collectors.toList()).toArray(new String[0]);
                Long[] postIds = (Long[]) sysUserPosts.stream().filter(r -> r.getDeptId().equals(sysUserDept.getDeptId()))
                        .map(SysUserPost::getPostId).collect(Collectors.toList()).toArray(new Long[0]);
                Long[] roleIds = (Long[]) sysUserRoles.stream().filter(r -> r.getDeptId().equals(sysUserDept.getDeptId()))
                        .map(SysUserRole::getRoleId).collect(Collectors.toList()).toArray(new Long[0]);
                userDeptVo.setAirportCodes(airportCodes);
                userDeptVo.setPostIds(postIds);
                userDeptVo.setRoleIds(roleIds);
                userDeptVo.setUserDeptTypeRefCode(sysUserDept.getUserDeptTypeRefCode());

                userDeptVo.setHiddenFlag(false);
                //非管理员的，只显示登录组织信息，防止分级管理员修改变更本人的 其他组织权限信息
                if (!SysUser.isAdmin(optUserId)
                        && (!loginDeptId.equals(userDeptVo.getDeptId())
                        && !deptList.stream().anyMatch(sysDept -> sysDept.getDeptId().equals(userDeptVo.getDeptId())))) {
                    userDeptVo.setHiddenFlag(true);
                }

                userDeptVoList.add(userDeptVo);
            }
        }
        return userDeptVoList;
    }

    @Override
    public List<SysUserDept> selectSysUserDeptListByIdCard(String idCard) {
        return sysUserDeptMapper.selectSysUserDeptListByIdCard(idCard);
    }
}
